package com.kkm.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.io.InputStream;

public class QiNiu {

    private static String accessKey = "4PZCorU3u2Od-UEA8D8WHXSAn1t1Apj-dgzFt2wj";
    private static String secretKey = "lcwcOyIIn9itQCT27jvMJmSl_4Esvjs80Qzn3lP4";
    private static String bucket = "feiyumusic";
    /**
     * 获取上传凭证
     */
    public static String getUploadCredential() {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        System.out.println(upToken);
        return upToken;
    }

    /**
     * 文件上传
     * @param zone
     *    华东	Zone.zone0()
     *    华北	Zone.zone1()
     *    华南	Zone.zone2()
     *    北美	Zone.zoneNa0()
     * @param upToken 上传凭证
     * @param localFilePath 需要上传的文件本地路径
     * @return
     */
    public static DefaultPutRet fileUpload(Zone zone, String upToken, String localFilePath) {
        // 构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(zone);
        // ...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        // 默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            // 解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            return putRet;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                // ignore
            }
        }
        return null;
    }
    public static String upload(InputStream inputStream) {
        System.out.println("1");
        String imgUrl = "";
        Configuration cfg = new Configuration(Zone.zone1());
        //创建上传对象
        UploadManager uploadManager = new UploadManager(cfg);
        String key = null;
        try {
            System.out.println("2");
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);
            System.out.println("3");
            try {
                Response response = uploadManager.put(inputStream, key, upToken, null, null);
                //解析上传成功的结果
                System.out.println("4");
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(222222);
                imgUrl =  putRet.key;
            } catch (QiniuException ex) {
                ex.printStackTrace();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return imgUrl;
    }

//    public static void main(String[] args) {
//
//        fileUpload(Zone.zone1(),QiNiu.getUploadCredential(),"src/main/webapp/aplay.jsp");
//    }
}
